home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / amiga / c_pr.a < prev    next >
Text File  |  1994-02-01  |  5KB  |  211 lines

  1.  
  2.         ;   C_PR.A  -pr option, no relocation info + resident
  3.         ;
  4.         ;   (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  5.         ;
  6.         ;   Amiga startup code for resident programs contained in a
  7.         ;   single CODE hunk.  BSS space is NOT part of the CODE hunk,
  8.         ;   only the initialized data (which is read-only).  We must
  9.         ;   allocate the data space
  10.         ;
  11.         ;   DLINK:
  12.         ;
  13.         ;   __BSS_LEN    : # long wrds of bss (occurs after DATA)
  14.         ;   __DATA_LEN    : # long wrds of initialized data to copy (if resident)
  15.         ;   __DATA_BAS    : base of data - read-only, PC-REL for -pr
  16.  
  17.         section text,code
  18.  
  19.         xref    __BSS_LEN        ; (dlink), length of BSS
  20.         xref    __DATA_BAS        ; (dlink), base of initialized data
  21.         xref    __DATA_LEN        ; (dlink), length of data
  22.  
  23.         xref    __main            ; we call _main()
  24.  
  25.         xref    _LVOSetSignal
  26.         xref    _LVOAllocMem
  27.         xref    _LVOFreeMem
  28.         xref    _LVOForbid
  29.         xref    _LVOReplyMsg
  30.  
  31.         xdef    __exit            ; we are _exit()
  32.         xdef    start
  33.         xdef    _SysBase        ; we export _SysBase
  34.         xdef    __WBMsg
  35.  
  36. start:
  37. ;;;;;;;;;;;;;;;;movem.l D2-D7/A2-A6,-(sp)       ;Not needed!
  38.  
  39.         move.l    4.W,A6            ; EXEC base
  40.  
  41.         move.l    sp,A2
  42.         move.l    A0,-(sp)            ; save arg for _main() call
  43.         move.l    D0,-(sp)            ; save arglen for _main() call
  44.  
  45.         ;   If we are flagged resident then there is NO BSS ALLOCATED
  46.         ;   If we are not flagged resident then BSS IS ALLOCATED AFTER DATA,
  47.         ;    BUT NOT CLEARED.
  48.  
  49.  
  50.         ;   Allocate BSS+DATA space and then copy static data.
  51.  
  52.         move.l    #__BSS_LEN,D0
  53.         add.l    #__DATA_LEN,D0
  54.         asl.l    #2,D0            ; x4
  55.         addq.l    #8,D0            ; MemList header extra
  56.         move.l    D0,D5            ; D5 = #bytes
  57.         moveq.l #0,D1
  58.         jsr    _LVOAllocMem(A6)
  59.         tst.l    D0
  60.         bne    alok
  61.         move.l    A2,sp
  62.         moveq.l #-1,D0
  63.         bra    exfail
  64.  
  65. alok        move.l    D0,A0
  66.         clr.l    (A0)+               ; MemList entry next ptr
  67.         move.l    D5,(A0)+            ; MemList entry #bytes
  68.         lea    32766(A0),A4        ; SET A4
  69.         lea    -8(A0),A3           ; A3 = MemList entry base
  70.                         ; can't copy to MemList(A4) yet
  71.  
  72.         ;   Copy data to allocated copy
  73.  
  74.                         ; A0 = dst
  75.         lea    __DATA_BAS(pc),A1   ; A1 = src
  76.         move.l    #__DATA_LEN,D0        ; D0 = long words
  77.         bra    bssent
  78. bsslop        move.l    (A1)+,(A0)+
  79. bssent        dbf    D0,bsslop
  80.         sub.l    #$10000,D0
  81.         bcc    bsslop
  82.  
  83. clrbss
  84.         ;   CLEAR BSS    &-32766(A4) + __DATA_LEN*4
  85.  
  86.         lea    -32766(A4),A0
  87.         move.l    #__DATA_LEN,D0
  88.         asl.l    #2,D0
  89.         add.l    D0,A0
  90.  
  91.         move.l    #__BSS_LEN,D0        ; longwords of bss
  92.         moveq.l #0,D1
  93.         bra    clrent
  94. clrlop        move.l    D1,(A0)+
  95. clrent        dbf    D0,clrlop
  96.         sub.l    #$10000,D0
  97.         bcc    clrlop
  98.  
  99.         move.l    A3,___MemList(A4)   ; memlist entry (if resident)
  100.         move.l    A2,__ExitSP(A4)     ; sp to restore
  101.  
  102.         moveq.l #0,D0            ; new signals
  103.         move.l    #$1000,D1        ; signal mask
  104.         jsr    _LVOSetSignal(A6)   ; clear ^C
  105.  
  106.         move.l    A6,_SysBase(A4)     ; resident segment.
  107.  
  108.         ;   Return value of 0 indicates succcess.  Flags
  109.         ;   have been set.
  110.  
  111.         jsr    __AutoInit0(pc)     ; A6 has SYSBase
  112.         bne    xfail
  113.         pea    1.W            ; autoconfig loader
  114.         jsr    __AutoConfig(pc)    ; note, rt pulls stack
  115.         addq.l    #4,sp
  116.         jsr    __AutoInit1(pc)     ; A6 has SYSBase
  117.         bne    xfail
  118.         jsr    __main(pc)
  119.  
  120.         ;   fall through to low level exit... this avoids referencing
  121.         ;   exit() if the user overides _main().
  122.  
  123. xfail
  124.         clr.l    -(sp)
  125.         clr.l    -(sp)
  126.  
  127.         ;   _EXIT()
  128.         ;
  129.         ;   since entry uses malloc we must free any incidental memory
  130.         ;   at __exit instead of _exit.
  131.         ;
  132.         ;   ReplyMsg(_WBMsg) just before returning
  133.  
  134. __exit:
  135.         pea    -1.W
  136.         jsr    __AutoConfig(pc)    ; note, rt pulls stack
  137.         addq.l    #4,sp
  138.         move.l    _SysBase(A4),A6
  139.         jsr    __AutoExit1(pc)     ; A6 has SysBase
  140.         jsr    __AutoExit0(pc)     ; A6 has SysBase
  141.  
  142.         move.l    __ExitSP(A4),A5     ; get sp... because we might free
  143.                         ; the space taken by the variable!
  144.  
  145.         move.l    __WBMsg(A4),D6      ; D6 = WBMsg if it exists
  146.  
  147.         move.l    ___MemList(A4),D0   ; free memory
  148.         beq    ex20
  149. ex10        move.l    D0,A2
  150.         move.l    (A2),A3             ; next...
  151.  
  152.         move.l    4(A2),D0            ; bytes
  153.         move.l    A2,A1            ; ptr
  154.         jsr    _LVOFreeMem(A6)
  155.  
  156.         move.l    A3,D0            ; next...
  157.         bne    ex10
  158. ex20
  159.  
  160.         move.l    4(sp),D0            ; get exit code
  161.         move.l    A5,sp            ; restore sp
  162.  
  163.         tst.l    D6            ; reply to workbench msg if it
  164.         beq    ex30            ; exists
  165.         jsr    _LVOForbid(A6)      ; forbid through exit
  166.         move.l    D6,A1
  167.         jsr    _LVOReplyMsg(A6)
  168.  
  169. ex30
  170.         ;   FINIS, poof.
  171.  
  172. exfail
  173. ;;;;;;;;;;;;;;;;movem.l (sp)+,D2-D7/A2-A6
  174.         rts
  175.  
  176.  
  177.         ;   Base of autoinit section
  178.  
  179.         section autoinit0,code
  180. __AutoInit0:
  181.         section autoinit1,code
  182. __AutoInit1:
  183.         section autoexit0,code
  184. __AutoExit0:
  185.         section autoexit1,code
  186. __AutoExit1:
  187.         section autoconfig,code
  188. __AutoConfig:
  189.  
  190.         ;   All library C code is compiled with the -S option
  191.         ;   which uses 'libdata' and 'libbss' section names,
  192.         ;   forcing library data to come before program data
  193.         ;   and library bss to come before program bss (because
  194.         ;   library data/bss sections are declared here first
  195.         ;   and sections of like name are coagulated).
  196.  
  197.         section libdata,data
  198. _Reserved    dc.l    0        ; force section to exist (dummy)
  199.  
  200.         section libbss,bss
  201.  
  202.         xdef    ___MemList    ; used by malloc/free
  203.         xdef    __ExitSP
  204.  
  205. _SysBase    ds.l    1
  206. __ExitSP    ds.l    1
  207. __WBMsg     ds.l    1
  208. ___MemList    ds.l    1
  209.  
  210.         END
  211.